package robot

import (
	"encoding/json"
	ms "gin_template/core/request"
	"gin_template/services/dataService/tianxing"
	"strings"

	"github.com/go-basic/uuid"
	"go.uber.org/zap"
)

type reqOption struct {
	ReqType    int        `json:"reqType"`
	Perception perception `json:"perception"`
	UserInfo   userinfo   `json:"userInfo"`
}
type perception struct {
	InputText inputText `json:"inputText"`
}
type userinfo struct {
	ApiKey string `json:"apiKey,omitempty"`
	UserId string `json:"userId,omitempty"`
}
type inputText struct {
	Text string `json:"text,omitempty"`
}

type apiResult struct {
	Results []result `json:"results,omitempty"`
}
type result struct {
	Values value `json:"values,omitempty"`
}
type value struct {
	Text string `json:"text,omitempty"`
}

func RequestRobot(config interface{}, info string) string {
	conf := config.(*tianxing.Config)
	option := &reqOption{
		ReqType: 0,
		Perception: perception{
			inputText{
				Text: info,
			},
		},
		UserInfo: userinfo{
			ApiKey: conf.Key,
			UserId: strings.Replace(uuid.New(), "-", "", -1),
		},
	}
	result := &apiResult{}
	res := ms.Request(conf.Url, &option, "application/json")
	err := json.Unmarshal(res, &result)
	if err != nil {
		zap.L().Info("RequestRobot序列化失败", zap.Error(err))
	}
	return result.Results[0].Values.Text
}
